<?php

/**
 * VideoModule actions.
 *
 * @package    ieasys
 * @subpackage VideoModule
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class videoActions extends sfActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request)
    {
        // $this->forward('default', 'module');
    }

    public function executeFirst() //sfWebRequest $request
    {
        //use_helper('connect');
        //echo connect();
        /*      $this->Catalogs = Doctrine_Core::getTable("Catalog")
        ->createQuery()
        ->execute();*/
        //$this->Videos=Doctrine_Core::getTable("Video")->createQuery()->execute();
        //$this->nums="哈哈哈哈和";
        //$this->pagevideos=Doctrine_Core::getTable("video")->createQuery()->execute();;//->createQuery('c')->orderBy('c.video_id desc')->execute();
    }

    public function executeDetail(sfWebRequest $request) //sfWebRequest $request
    {
        $this->videoId = $request->getParameter('videoId');
        $this->catalogId = $request->getParameter('catalogId');
        if (null == $this->videoId || "" == $this->videoId)
            $this->videoId = 1000;
        if (null == $this->catalogId || "" == $this->catalogId)
            $this->catalogId = 1000;
        //        $this->Videos = @Doctrine_Core::getTable("video")->createQuery()->where("video_id=" . $this->videoId)->limit(1)->execute();

        $this->video = Doctrine_Core::getTable("Video")->find($this->videoId);
        $this->group_videos = array();
        if ($this->video->getVideoGroup()) {
            $this->getLogger()->log("group id:" . $this->video->getVideoGroup()->getVideoGroupId());
            //            $links = $this->video->getVideoGroup()->getGroupVideoLink();
            $groupId = $this->video->getVideoGroup()->getVideoGroupId();
            $links = Doctrine_Core::getTable('GroupVideoLink')->createQuery('a')
                    ->addWhere('a.video_group_id = ?', $groupId)
                    ->addOrderBy('a.order_i')->execute();
            foreach ($links as $link) {
                $this->group_videos[] = array($link->getVideo(), $link->getVideoId());
            }
        }
        $this->response->setTitle($this->video->getTitle() . "_视频_" . $this->video->getCatalog()->getDescription() . "_" . sfConfig::get("app_site_name"));
        $z = $this->video->getKeyword();
        $this->response->setKeywords($z);

        //add by sam 2011-04-02 13:43
        $dbBase = new DbBase();
        $this->row_array = array();
        $this->contents = array();
        $sql = "update video set hit_i = (case hit_i when NULL then 1 else hit_i+1 end) where status_id !=1006 and video_id = " . $this->videoId;
        //        $this->row_array = $dbBase->getDataQuery($sql);

        try {
            @Doctrine_Manager::connection()->execute($sql);
        } catch (Exception $e) {
            echo $e;
        }

        $sql = "select publish_time as currentTime, source,artist,tag,hit_mask_i,hit_i  from video where status_id !=1006 and video_id = " . $this->videoId;

        $this->row_array = $dbBase->getDataQuery($sql);
        if (count($this->row_array) > 0) {
            $this->row_array = $this->row_array[0];
        }
    }

    public function executeList(sfWebRequest $request) //sfWebRequest $request
    {
        $routing = $this->getContext()->getRouting();
        $this->pageUrl = $routing->getCurrentInternalUri();
        $currentPageNumber = 1;
        $recordCount = 21;
        if ($request->getParameter("pageNumber") != null)
            $currentPageNumber = $request->getParameter("pageNumber");
        $this->catalogId = $request->getParameter('catalogId');
        $this->subCatalogId = $request->getParameter('subCatalogId');
        $this->currentPageNumber = $currentPageNumber;
        $currentRowNumber = ($currentPageNumber - 1) * $recordCount;
        $sqlStr = "select video_id, title, snapshot_big,artist,duration, publish_time, v.catalog_id from video v "; //"where status_id=1005 and catalog_id=".$this->catalogId." and sub_catalog_id=".$this->subCatalogId
        if ($this->subCatalogId == 1011) //热门推荐
            $sqlStr = $sqlStr . " inner join recommendContent r on v.video_id=r.content_id where r.content_type_i = 1001 and";
        else
            $sqlStr = $sqlStr . " where ";
        $sqlStr = $sqlStr . " status_id=1005 ";
        $title = "";
        if ($this->subCatalogId != null && $this->subCatalogId != 1011) {
            $catalog = Doctrine_Core::getTable("catalog")->find("$this->subCatalogId");
            $title = $catalog->getDescription() . "_";
            $sqlStr = $sqlStr . " and sub_catalog_id=" . $this->subCatalogId;
        }
        else if ($this->subCatalogId == 1011)
            $title = "热门推荐_";
        if ($this->catalogId != null) {
            $sqlStr = $sqlStr . " and v.catalog_id=" . $this->catalogId;
            $catalog = Doctrine_Core::getTable("catalog")->find("$this->catalogId");
            $title = $title . "视频_" . $catalog->getDescription();
        }
        $this->response->setTitle($title . "_" . sfConfig::get("app_site_name"));
        $this->newsArray = DbBase::getDataQuery($sqlStr);
        $rowNumber = count($this->newsArray);
        if ($rowNumber % $recordCount == 0)
            $this->totalPageNumber = $rowNumber / $recordCount;
        else
        {
            $tempStr = $rowNumber / $recordCount;
            $tempStr = substr($tempStr, 0, strrpos($tempStr, ".", 0));
            $this->totalPageNumber = $tempStr + 1;
        }
        //$sqlStr = $sqlStr." order by publish_time desec, video_id desc";
        //        $sqlStr = "with newQuery as(select row_number() over(order by publish_time desc, video_id desc) as rowNumber," . substr($sqlStr, 6) . ") select top " . $recordCount . " * from newQuery where rowNumber > " . $currentRowNumber;
        $sqlStr = $sqlStr . " order by publish_time desc, video_id desc limit " . $currentRowNumber . "," . $recordCount;
        $this->videos = array();
        $this->videos = DbBase::getDataQuery($sqlStr);
        //        $query = Doctrine_Query::create()->from("Video v")
        //                ->Where('catalog_id='.$this->catalogId)
        //                ->andWhere('sub_catalog_id='.$this->subCatalogId)
        //                ->orderBy('v.publish_time desc,v.video_id desc');
        //        if(1011==$this->subCatalogId)
        //        {
        //            $query = Doctrine_Query::create()->from("Video")
        //
        //                   //->andWhere('sub_catalog_id='.$this->subCatalogId)
        //                   ->addFrom("RecommendContent")
        //                   ->Where('catalog_id='.$this->catalogId)
        //                   ->andwhere("content_type_i = 1001")
        //->addPendingJoinCondition("r","content_id=v.video_id");


        /*$query = Doctrine_Query::create()
              ->from("news n")
              ->addFrom("FocusContent f")
              ->where("f.content_type_i = ?",1000)

              ->addPendingJoinCondition("f","f.content_id=n.news_id");
              //->orderBy("f.order_i desc");

      $this->page = new sfDoctrinePager('FocusContent',1);
        $this->page->setQuery($query);  //$q   数据库查询语句
        $this->page->setPage($request->getParameter('page', 1));
        $this->page->init();*/
        //$query = Doctrine_Query::create()->from("Video v")
        //->leftJoin('RecommendContent r')->
        // ->addFrom('RecommendContent as r')
        //->where('r.content_type_i=1003')
        //->addPendingJoinCondition("r","v.video_id=r.content_id")
        //->orderBy('r.order_i desc')
        ;

        //}
        //        $this->pages = new sfDoctrinePager('Video',$this->recordCount);//$this->currentPage
        //        $this->pages->setQuery($query);  //$q   数据库查询语句
        //        $this->pages->setPage($this->currentPage);//
        //        $this->pages->init();
    }

    function getDataRows($sqlStr)
    {
        $dbBase = new DbBase();
        return $dbBase->getDataQuery($sqlStr);
    }

}
